<?php
namespace App\Models\V6;

use DB;
use App\Models\V6\MemberModel;


class DrpModel extends MemberModel{

    /**
     * 检查是否是学习官
     * */
    public function get_drp_info_by_member_number($memberNumber,$isClo = false){
        $drpInfo = DB::table('memberdrp')
            ->where('DeleteMark', '1')
            ->where('MemNumber', $memberNumber);
        if($isClo){
            $drpInfo->where('DRPLevel',1);
        };
        $drpInfo = $drpInfo->select('DRPID')->first();
        if($drpInfo){
            return $this->get_drp_info_by_drpid($drpInfo->DRPID,$isClo);
        }else{
            return false;
        }
    }



    /**
     * 获取用户信息
     * 根据学习官ID获取会员信息
     * @param integer $drpId 学习官信息ID
     * @param boolean $isClo 是否是学习官
     * @return Object
     * */
    public function get_drp_info_by_drpid($drpId,$isClo = false){
        $drpInfo = DB::table('memberdrp as a')
            ->leftJoin('memberinfo as b','b.MemNumber','=','a.MemNumber')
            ->where('a.DeleteMark', '1')
            ->where('a.DRPID', $drpId)
            ->select('a.*','b.FilePath1','b.YourName','b.UserID',"b.WeiXID");

        if($isClo){
            $drpInfo->where('a.DRPLevel',1);
        }

        $drpInfo = $drpInfo->first();

        if($drpInfo){
            $drpInfo->FilePath1 = change_member_head_img($drpInfo->FilePath1);
        }else{
            $drpInfo = false;
        }
        return $drpInfo;
    }


    /**
     * 通过用户ID获取学习官信息
     * @param integer $userId 用户ID
     * @param boolean $isClo 是否要求是学习官
     * @return Object || false
     * */
    public function get_drp_info_by_user_id($userId,$isClo = false){
        $db = DB::table('memberinfo as a ')
            ->leftJoin('memberdrp as b','b.MemNumber','=','a.MemNumber')
            ->where('a.UserID',$userId)
            ->where('b.DeleteMark',1);

        if($isClo){
            $db->where('b.DRPLevel',1);
        }
        $drpId = $db->value('b.DRPID');
        if($drpId > 0){
            $drpInfo = $this->get_drp_info_by_drpid($drpId,$isClo);
        }else{
            $drpInfo = false;
        }
        return $drpInfo;
    }




    /**
     * 添加学习官的会员
     * @param studentData array 学习官信息
     * */
    public function add_drp_student(array $studentData){
        $drpId     = $studentData['ParentID'];
        $drpInfo   = $this->get_drp_info_by_drpid($drpId);

        if($drpInfo){
            $studentData["IDTree"] = $drpInfo->IDTree;
        }
        $dateTime   = date_time();
        $studentData['ReleaseDate'] = $dateTime;
        $studentData['AddDate']     = $dateTime;
        $studentData['DeleteMark']  = 1;

        $result  = DB::table('memberdrp')->insertGetId($studentData);
        return $result;
    }

    /**
     * 添加学习官下面的会员
     * @param integer $drpId 学习官ID
     * @param string $studentMemberNumber 新会员编号
     * @param $isClo   boolean 是否是学习官
     * @return boolean
     * */
    public function add_drp_child($studentMemberNumber,$drpId=0,$isClo=false){

        $drpLevel = $isClo ? 1 : 2;
        $idTree = ".";
        if($drpId > 0){
            $drpInfo   = $this->get_drp_info_by_drpid($drpId);
            if($drpInfo){
                $idTree = $drpInfo->IDTree;
            }
        }
        $dateTime   = date_time();


        //查找系统中是否已有编号
        $drpInfo = DB::table('memberdrp')
            ->where('MemNumber',$studentMemberNumber)
            ->where('DeleteMark',1)
            ->first();

        $studentData = [
            'ReleaseDate'   => $dateTime,
            'DRPLevel'      => $drpLevel,
            'ParentID'      => $drpId
        ];

        if($drpInfo)
        {
            $studentData['IDTree']  = $idTree.$drpInfo->IDTree.".";
            DB::table('memberdrp')->where('MemNumber',$studentMemberNumber)->update($studentData);
        }
        else
        {
            $studentData2 = [
                'MemNumber'     => $studentMemberNumber,
                'AddDate'       => $dateTime,
                'DRPScale'      => 0
            ];
            $studentData = array_merge($studentData,$studentData2);
            $newDrpId  = DB::table('memberdrp')->insertGetId($studentData);
            $idTree .= $newDrpId.".";
            DB::table('memberdrp')->where('DRPID',$newDrpId)->update(["IDTree"=>$idTree]);
        }
        return true;
    }




    /**
     * 获得父级学习官
     * @param integer $userId 用户ID
     * @param integer $grade 父ID等级
     * @return array
     * */
    public function get_parent_drp_list($userId,$grade=1){
        $parentsInfo = [];
        $drpInfo = $this->get_member_drp_info($userId);
        if(!$drpInfo){
            return $parentsInfo;
        }

        $drpId   = $drpInfo->ParentID;
        while($grade > 0 && $drpId > 0){
            $parentInfo = $this->get_drp_info_by_drpid($drpId);

            array_push($parentsInfo,$parentInfo);
            $grade--;
            $drpId   = $parentInfo->ParentID;
        }
        return $parentsInfo;
    }


    /**
     * 记录学习官的花费记录
     * @param $userId integer 用户ID
     * @param $money integer 花费数量
     * @param $orderId integer 订单ID
     * @param $note string 备注信息
     * */
    static function out_money_log($userId,$money,$orderId,$note=''){
        $data = [
            "user_id"   => $userId,
            "money"     => $money,
            "order_id"  => $orderId,
            "note"      => $note,
            "add_time"  => date_time()
        ];

        DB::table('clo_out_money_log')->insert($data);
    }


    /**
     * 学习官收入记录
     * @param $memberNumber string 用户编号
     * @param $orderId integer 订单ID
     * @param $totalMoney integer 支付的总金额
     * @param $scale integer 返点比例
     * @param $note string 备注
     * */
    static function income_money_log($memberNumber,$orderId,$totalMoney,$scale,$note){
        $scale  = $scale/100;
        $time   = date_time();
        $data = [
            'ToMemNumber'   => $memberNumber,
            'order_id'      => $orderId,
            'TotalMoney'    => $totalMoney,
            'Scale'         => $scale,
            'PayMoney'      => $totalMoney*$scale,
            'PushMoneyDesc' => $note,
            'ReleaseDate'   => $time,
            'AddDate'       => $time
        ];
        DB::table('pushmoneylog')->insert($data);
    }
}